package coms.ejb;

import Util.GenericReviewStrategy;
import Util.MapUtil;
import Util.ReviewStrategy;
import Util.XMLUtil;
import coms.dataaccess.dao.ConferenceItemDAO;
import coms.dataaccess.dao.PublicationDAO;
import coms.dataaccess.dao.ReviewDAO;
import coms.dataaccess.hibernate.HibernateDAOFactory;
import coms.dto.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;


@Stateless (mappedName="PublicationBean")
public class PublicationBean extends GenericBean<Publication> implements PublicationBeanRemote{
    
  private PublicationDAO publicationDAO;
  private ReviewDAO reviewDAO;
    @PostConstruct
    public void construct(){
        super.construct();
        HibernateDAOFactory factory = new HibernateDAOFactory();
        publicationDAO = factory.getPublicationDAO();
        reviewDAO = factory.getReviewDAO();
    }
    
     @Override
    public List<Publication> getByConferenceItem(ConferenceItem conferenceItem) {
          Publication publication = new Publication();
          publication.setConferenceItem(conferenceItem);
          List<Publication> publicationList = new ArrayList<Publication>();
        try {
            publicationList = publicationDAO.search(publication);
        } catch (SQLException ex) {
            Logger.getLogger(Publication.class.getName()).log(Level.SEVERE, null, ex);
        }
         return publicationList;
    }
    
    @Override
    public List<Publication> getByFirstAuthor(User user) {
          Publication publication = new Publication();
          publication.setFirstAuthor(user);
          List<Publication> publicationList = new ArrayList<Publication>();
        try {
            publicationList = publicationDAO.search(publication);
        } catch (SQLException ex) {
            Logger.getLogger(Publication.class.getName()).log(Level.SEVERE, null, ex);
        }
         return publicationList;
    }
    

    public List<Publication> getByAnyAuthorType(User author) {
          List<Publication> publicationList = new ArrayList<Publication>();

            publicationList = publicationDAO.searchByAnyAuthorType(author);
   
         return publicationList;
    }
    
   
    public void processConferencePublicationReviews(Conference conference)
    {
        for (ConferenceItem item : conference.getConferenceItemList()) {
            processConferenceItemPublicationReviews(item);
        }
    }
    public void processConferenceItemPublicationReviews(ConferenceItem item) {
        System.out.println("item"+item.getId());
        Map<Publication,Double> publicationRatings = new HashMap<Publication,Double>();
        try{
            ConferenceItemDAO dao  = new HibernateDAOFactory().getConferenceItemDAO();
            item  = dao.getById(item.getId());
            List<Publication> publications = item.getPublications();
            System.out.println("publications   "+publications.size());
            for (Publication publication : publications) {
                  System.out.println("............");
                  double rating = processPublication(publication);
                  System.out.println("publication id  - rating>"+publication.getId() + "-" + rating);
                  publicationRatings.put(publication,new Double(rating));
                  publication.setRating(rating);
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        int lastIndex = publicationRatings.size()*30/100;
        if(lastIndex<3)
        {
            lastIndex = publicationRatings.size();
        }
        System.out.println("lastindex"+lastIndex);
        publicationRatings = MapUtil.processMap(publicationRatings,lastIndex);
        System.out.println("process map size"+publicationRatings.size());
        System.out.println("Sorted Map......");
        for(Double rating : publicationRatings.values())
        {
            System.out.println("rating :"+rating);            
        }
        
        
        for(Publication publication : publicationRatings.keySet() )
        {
            publication.setStatus(Publication.SELECTED);
            
            System.out.println("publication - "+publication.getId());
            try{
                
               boolean b= publicationDAO.update(publication);
               System.out.println("update - " + b);
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        
    }
    
    private double processPublication(Publication publication)
    {
        System.out.println("processPublication");
        List<Review> reviews = publication.getReviews();
        ReviewStrategy strategy = new GenericReviewStrategy();
        int totalPoints=0;
        double points;
        System.out.println("totalpoints"+totalPoints);
        try{
            for (Review review : reviews) {
                  String reviewXML = review.getReviewXML();
                  List<Section> sections =  XMLUtil.getReviewSectionListFromXLMStr(reviewXML);
                  totalPoints +=strategy.calulatePoint(sections);
                  System.out.println("totalPoints" + totalPoints);
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        if(reviews.size()>0)
        {
            points = totalPoints/(reviews.size());
        }else points=0;
        return points;
    }


}
